package com.ja.openchat.viewmodels;

import android.app.Application;

import androidx.annotation.NonNull;

import com.ja.openchat.databinding.ActivityEditGroupChatBinding;
import com.ja.openchat.ui.chat.group.EditGroupChatActivity;
import com.zy.openchat.core.manager.TelegramManager;
import com.zy.openchat.core.manager.TgEvent;
import com.zy.openchat.core.model.group.SuperGroupFullInfo;
import com.zy.openchat.core.model.group.SuperGroupInfo;
import com.zy.openchat.core.model.message.ChatInfo;
import com.zy.openchat.core.model.message.MessageInfo;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import me.goldze.mvvmhabit.base.BaseModel;
import me.goldze.mvvmhabit.base.BaseViewModel;


public class EditGroupViewModel extends BaseViewModel {

    public EditGroupChatActivity editGroupChatActivity;
    private ActivityEditGroupChatBinding binding;

    public EditGroupViewModel(@NonNull Application application, BaseModel mRepository) {
        super(application);
    }

    public void setContext(EditGroupChatActivity editGroupChatActivity, ActivityEditGroupChatBinding binding) {
        this.editGroupChatActivity = editGroupChatActivity;
        this.binding = binding;
    }



    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onTgEvent(final TgEvent tg_event) {
        switch (tg_event.event) {
            case EUser_Td_Chat_Title_Changed: {
                //会话标题修改通知
                ChatInfo chat = (ChatInfo) tg_event.data;
                if (editGroupChatActivity.chatInfo == chat) {
                    editGroupChatActivity.chatInfo.title = chat.title;
                    editGroupChatActivity.chatInfo.title_full_py = chat.title_full_py;
                    editGroupChatActivity.resetUI();
                }
            }
            break;
            case EUser_Td_Chat_Permissions_Changed: {
                //会话权限变更通知
                ChatInfo chat = (ChatInfo) tg_event.data;
                if (editGroupChatActivity.chatInfo == chat) {
                    editGroupChatActivity.chatInfo.permissions = chat.permissions;
                    editGroupChatActivity.resetUI();
                }
            }
            break;
            case EUser_Td_Chat_Photo_Changed:
            case EUser_Td_Group_Photo_Ok: {
                //会话头像修改通知
                //群组头像下载完成
                ChatInfo chat = (ChatInfo) tg_event.data;
                if (editGroupChatActivity.chatInfo == chat) {
                    editGroupChatActivity.chatInfo.photo = chat.photo;
                    editGroupChatActivity.resetUI();
                }
            }
            break;
            case EUser_Td_Contact_Photo_Ok: {
                //联系人头像更新
                editGroupChatActivity.memberIsManagerGridAdapter.notifyDataSetChanged();
            }
            break;
            case EUser_Td_Group_Super_Info_Changed: {
                SuperGroupInfo info = (SuperGroupInfo) tg_event.data;
                if (info.id == editGroupChatActivity.chatInfo.superGroupId()) {
                    editGroupChatActivity.super_groupInfo = info;
                    editGroupChatActivity.resetMamagerMembersListUI();
                    editGroupChatActivity.resetUI();
                }
            }
            break;
            case EUser_Td_Group_Super_Full_Info_Changed: {
                long id = (long) tg_event.dataEx;
                SuperGroupFullInfo info = (SuperGroupFullInfo) tg_event.data;
                if (id == editGroupChatActivity.chatInfo.superGroupId()) {
                    //延时请求群组成员
                    binding.headerImageView.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            editGroupChatActivity.super_groupFullInfo = info;
                            editGroupChatActivity.resetMamagerMembersListUI();
                            editGroupChatActivity.resetUI();

                            //刷新列表
                            editGroupChatActivity.getSuperMembers();
                            editGroupChatActivity.getSuperAdminMembers();
                        }
                    }, 100);
                }
            }
            break;
            case EUser_Td_Chat_Send_Message_Success:
            case EUser_Td_Chat_Send_Message_Fail: {
                MessageInfo msg = (MessageInfo) tg_event.data;
                long oldMsgId = (long) tg_event.dataEx;
                if (msg != null && editGroupChatActivity.sendingMsg != null && editGroupChatActivity.sendingMsg.id == oldMsgId) {
                    if (msg.sendState() == TelegramManager.MessageSendState.MessageSendState_Success) {
                        editGroupChatActivity.setGroupPinnedMessage_step2(msg.id);
                    } else {
                        showErrorTip("群公告设置失败，请稍后重试");
                    }
                }
            }
            break;
        }
    }
}
